package com.atguigu.spzx.manage.controller;

import com.atguigu.spzx.manage.service.SysUserService;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/admin/system/sysUser")
@Slf4j
@Tag(name = "用户管理接口")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService ;
    @Operation(summary = "用户管理分页查询")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    //keyword,createTimeBegin,createTimeEnd,pageNum,pageSize
    public Result<PageInfo<SysUser>> findByPage(SysUserDto sysUserDto ,
                                                @PathVariable(value = "pageNum") Integer pageNum ,
                                                @PathVariable(value = "pageSize") Integer pageSize) {
        log.info("用户查询内容:{},页码:{},页数:{}",sysUserDto,pageNum,pageSize);
        PageInfo<SysUser> pageInfo = sysUserService.findByPage(sysUserDto , pageNum , pageSize) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "添加用户")
    @PostMapping(value = "/saveSysUser")
    //userName,password,name,phone,avatar,description,status
    public Result saveSysUser(@RequestBody SysUser sysUser) {
        log.info("添加的角色:{}",sysUser);
        sysUserService.saveSysUser(sysUser) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "更新用户")
    @PutMapping(value = "/updateSysUser")
    //userName,password,name,phone,avatar,description,status
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        log.info("修改用户内容:{}",sysUser);
        sysUserService.updateSysUser(sysUser) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    @Operation(summary = "删除用户")
    @DeleteMapping(value = "/deleteById/{userId}")
    //userid
    public Result deleteById(@PathVariable(value = "userId") Long userId) {
        sysUserService.deleteById(userId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }
    @DeleteMapping("/deleteSysUser")
    public Result deleteSysUser(@PathVariable Long userId){
        sysUserService.deleteById(userId);
        return Result.build("",ResultCodeEnum.SUCCESS);
    }

}
